<?php
/* explain:		
 * Project:     
 * File:        vedioUpload.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");
include_once("constant/CommonClass.php");

class ContentImpl extends CommonUtils
{
	
	function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag = $DBTag;
	}

	public function getById($id) 
	{
		$sql = "SELECT name, description, status, createdTime, modifiedTime, r_imageCount, recommend FROM content WHERE id = $id";
		
		$dto 	= null;
		$conn 	= null;
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($rs && $row = mysql_fetch_row($rs)) {
				$dto = new ContentDto();

				$dto->id 			= $id;

				$dto->name			= $row[0];
				$dto->description	= $row[1];
				$dto->status		= $row[2];
				$dto->createdTime	= $row[3];
				$dto->modifiedTime	= $row[4];
				$dto->r_imageCount	= $row[5];
				$dto->recommend		= $row[6];
				
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		if($conn != null)
			$this->freeConnection($this->DBTag, $conn);
			
		return $dto;
	}
	
	/* 
	 *
	 * @param  string 	name 
 	 * @return int    	true: insert ID
	 */
    public function add($dto)
	{
		if($dto instanceof ContentDto){			
			$sql 	 = "INSERT INTO content SET createdTime = now(), name = '";

			$sql	.= addslashes($dto->name);
	
			$sql 	.= "', description='";
			$sql	.= addslashes($dto->description);

			$sql 	.= "', comments='";
			$sql	.= addslashes($dto->comments);

			$sql	.= "', status=";
			$sql	.= $dto->status;
			
			$sql	.= ", r_imageCount=";
			$sql	.= $dto->r_imageCount;

			$sql	.= ", recommend=";
			$sql	.= $dto->recommend;
			
			$id = $this->insert($this->DBTag, $sql);
			
			$dto->id = $id;			
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of ContentDto.';
		}
			
		return $id;
	}
	
	public function addCategory($contentId, $categoryId)
	{
		$sql = "INSERT INTO content_category SET contentId = $contentId, categoryId = $categoryId  ON DUPLICATE KEY UPDATE categoryId = Values(categoryId)";		
		$this->update($this->DBTag, $sql);
	}
	
	/*
	 * @param $categories array
	 */
	public function addCategories($contentId, $categories)
	{
		$sql = "DELETE FROM content_category WHERE contentId = $contentId";
		
		$this->update($this->DBTag, $sql);
		
		foreach($categories as $catId){
			$this->addCategory($contentId, $catId);	
		}		
	}
	
	public function addImage($contentId, $imageId)
	{
		$sql = "INSERT INTO content_images SET contentId = $contentId, imageId = $imageId  ON DUPLICATE KEY UPDATE imageId = Values(imageId)";		
		$this->update($this->DBTag, $sql);
		
		$imgCount = $this->getImageCount($contentId);
				
		$sql = "UPDATE content SET r_imageCount = $imgCount WHERE id = $contentId";	
		return $this->update($this->DBTag, $sql);
	}
	
	public function delCelebrity($contentId)
	{
		$sql = "DELETE FROM content_celebrity WHERE contentId = $contentId";
		
		$this->update($this->DBTag, $sql);
	}
	
	public function addCelebrity($contentId, $celebrityId)
	{		
		$sql = "INSERT INTO content_celebrity SET contentId = $contentId, celebrityId = $celebrityId  ON DUPLICATE KEY UPDATE celebrityId = Values(celebrityId)";		
		$this->update($this->DBTag, $sql);
	}
	
	public function addTag($contentId, $tagId)
	{		
		$sql = "INSERT INTO content_tags SET contentId = $contentId, tagId = $tagId  ON DUPLICATE KEY UPDATE tagId = Values(tagId)";		
		$this->update($this->DBTag, $sql);
	}
	
	public function delTags($contentId)
	{		
		$sql = "DELETE FROM content_tags WHERE contentId = $contentId";		
		$this->update($this->DBTag, $sql);
	}
	
	/* modify
	 *
	 * @param  string 	name 
 	 * @param  return 	boolean
	 */
	public function edit($dto)
	{
		$isOk = false;
		
		if($dto instanceof ContentDto){
			$sql 	 = "UPDATE content SET name='";
			$sql	.= addslashes($dto->name);
	
			$sql 	.= "', comments='";
			$sql	.= addslashes($dto->comments);
	
			$sql 	.= "', description='";
			$sql	.= addslashes($dto->description);

			$sql	.= "', status=";
			$sql	.= $dto->status;
			
			$sql	.= ", r_imageCount=";
			$sql	.= $dto->r_imageCount;
			
			$sql	.= ", recommend=";
			$sql	.= $dto->recommend;
			
			$sql	.= " WHERE id =";
			$sql	.= $dto->id;
			
			$updateCount = $this->update($this->DBTag, $sql);
			
			$isOk = true;			
		}else{			
			$this->message = 'input object is not instance of ContentDto.';
		}		

		return $isOk;
	}
		
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM content WHERE id = $id";
		
		$this->update($this->DBTag, $sql);
		
		$sql = "DELETE FROM content_images WHERE contentId = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	public function deleteImage($contentId, $imageId)
	{
		$sql = "DELETE FROM content_images WHERE contentId = '$contentId' AND imageId='$imageId'";

		$this->update($this->DBTag, $sql);
		
		$imgCount = $this->getImageCount($contentId);
				
		$sql = "UPDATE content SET r_imageCount = $imgCount WHERE id = $contentId";	
		return $this->update($this->DBTag, $sql);
	}
	
	public function isNotExisted($contentId, $imageId)
	{
		$imageCount = $this->getColumn($this->DBTag, "SELECT count(*) FROM content_images WHERE contentId != $contentId AND imageId = $imageId");
		if(empty($imageCount))
			return true;
		else
			return false;
	}
	
	public function getImageCount($contentId)
	{
		$imageCount = $this->getColumn($this->DBTag, "SELECT count(*) FROM content_images WHERE contentId = $contentId");
		if(empty($imageCount))
			$imageCount = 0;
			
		return $imageCount;
	}
	
	public function getCelebrityId($contentId)
	{
		$sql = "SELECT celebrityId FROM content_celebrity WHERE contentId = $contentId";
		
		return $this->getColumn($this->DBTag, $sql);
	}
	
	public function getCelebrityName($contentId)
	{
		$sql = "SELECT celebrityId FROM content_celebrity WHERE contentId = $contentId";
		
		$celebrityId = $this->getColumn($this->DBTag, $sql);
		
		if(empty($celebrityId))
			return '';
		else{
			$sql = "SELECT name FROM celebrity WHERE id = $celebrityId";
		
			return $this->getColumn($this->DBTag, $sql);
		}
	}
	
	public function getTags($contentId)
	{		
		$sql = "SELECT group_concat(name) name FROM tags WHERE status = 1 AND id IN (SELECT tagId FROM content_tags WHERE contentId = $contentId)";
		
		return $this->getColumn($this->DBTag, $sql);
	}
	
	public function getCategoryIds($contentId)
	{
		$sql = "SELECT categoryId FROM content_category WHERE contentId = $contentId";
		
		return $this->getColumns($this->DBTag, $sql);
	}
	
	public function getImages($contentId, $order = 'ORDER BY level')
	{
		$arrback = array();
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$sql  = 'SELECT width, height, extName, fileSize, name, description, id, md5, status, level ';
			$sql .= "FROM images WHERE id IN (SELECT imageId FROM content_images WHERE contentId = $contentId) $order";
					
			$rs  = mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
			while($rs && $row = mysql_fetch_array($rs)){
				$dto = new ImageDto();

				$dto->width			= $row[0];
				$dto->height		= $row[1];
				$dto->extName		= $row[2];
				$dto->fileSize		= $row[3];
				$dto->name			= $row[4];
				$dto->description	= $row[5];
				$dto->id 			= $row[6];
				$dto->md5			= $row[7];
				$dto->status		= $row[8];
				$dto->level			= $row[9];
				
				$arrback[] = $dto;
			}
			
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);
		
		return $arrback;
	}
	
	public function getTagIdsStr($contentId, $glue = ',')
	{
		$sql 	= "SELECT tagId FROM content_tags WHERE contentId = $contentId";
		$arrIds = $this->getColumns($this->DBTag, $sql);
		
		return implode($glue, $arrIds);
	}
	
	public function getRelatedImagePage($contentId, $page = 0, $discount = 20)
	{
		$tagIds = $this->getTagIdsStr($contentId);
		if(empty($tagIds))
			return array();
		
		$order	= " ORDER BY level";
		$where 	= "WHERE status = 1 AND id IN (SELECT imageId FROM image_tags WHERE tagId IN ($tagIds))";
		
		return $this->getCustomImages($contentId, $where, $order, $page, $discount);
	}
	
	public function getImagePage($contentId, $page = 0, $discount = 20)
	{
		$order	= "ORDER BY level, id DESC";
		$where 	= "WHERE status = 1 AND id IN (SELECT imageId FROM content_images WHERE contentId = $contentId)";
		
		return $this->getCustomImages($contentId, $where, $order, $page, $discount);
	}
	
	public function getImageIds($contentId)
	{
		$sql 	= "SELECT id FROM images WHERE status = 1 AND id IN (SELECT imageId FROM content_images WHERE contentId = $contentId) ORDER BY level, id DESC";
		
		return $this->getColumns($this->DBTag, $sql);
	}
	
	public function getAllImages($contentId)
	{
		$order	= "ORDER BY level, id DESC";
		$where 	= "WHERE status = 1 AND id IN (SELECT imageId FROM content_images WHERE contentId = $contentId)";
		
		return $this->getCustomImages($contentId, $where, $order, 1, 10000);
	}
	
	public function getCustomImages($contentId, $where, $order, $page = 0, $discount = 20)
	{
		$arrback 	= array();
		$conn		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$sql 	= "SELECT count(1) FROM images $where";
			
			$rs  	= mysql_query($sql, $conn);
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if($this->isHaveData){				
				$sql  = 'SELECT width, height, extName, fileSize, name, description, id, md5, status, level, isAnimated ';
				$sql .= "FROM images $where $order";
				
				if($discount > 0)
					$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
				while($rs && $row = mysql_fetch_array($rs)){
					$dto = new ImageDto();
	
					$dto->width			= $row[0];
					$dto->height		= $row[1];
					$dto->extName		= $row[2];
					$dto->fileSize		= $row[3];
					$dto->name			= $row[4];
					$dto->description	= $row[5];
					$dto->id 			= $row[6];
					$dto->md5			= $row[7];
					$dto->status		= $row[8];
					$dto->level			= $row[9];
					$dto->isAnimated	= $row[10];
					
					$arrback[] = $dto;
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);	
		
		return $arrback;
	}
	
	public function getPopularImages($page = 1, $discount = 20)
	{
		$arrback 	= array();
		$conn		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			
			$sql 	= "SELECT count(1) FROM images i, content c, content_images ci  where i.status = 1 AND i.id = ci.`imageId` AND ci.`contentId` = c.id";			
			$rs  	= mysql_query($sql, $conn);
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if($this->isHaveData){
				$sql  = 'SELECT i.width, i.height, i.extName, i.fileSize, i.name, i.description, i.id, i.md5, i.status, i.level, i.isAnimated ';
 				$sql .= 'FROM images i, content c, content_images ci  where i.status = 1 AND i.id = ci.`imageId` AND ci.`contentId` = c.id order by c.`r_clicked` desc';
				
				if($discount > 0)
					$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
				while($rs && $row = mysql_fetch_array($rs)){
					$dto = new ImageDto();
	
					$dto->width			= $row[0];
					$dto->height		= $row[1];
					$dto->extName		= $row[2];
					$dto->fileSize		= $row[3];
					$dto->name			= $row[4];
					$dto->description	= $row[5];
					$dto->id 			= $row[6];
					$dto->md5			= $row[7];
					$dto->status		= $row[8];
					$dto->level			= $row[9];
					$dto->isAnimated	= $row[10];
					
					$arrback[] = $dto;
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);	
		
		return $arrback;
	}
	
	public function getPopularContents($page = 1, $discount = 20)
	{
		$arrback 	= array();
		$conn		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			
			$sql 	= "SELECT count(1) FROM images i, content c, content_images ci  where i.status = 1 AND i.id = ci.`imageId` AND ci.`contentId` = c.id";			
			$rs  	= mysql_query($sql, $conn);
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if($this->isHaveData){
				$sql  = 'SELECT i.width, i.height, i.extName, i.fileSize, i.name, i.description, i.id, i.md5, i.status, i.level, i.isAnimated, ';
				$sql .= 'c.id cid, c.name, c.description, c.status, c.createdTime, c.modifiedTime, c.r_imageCount, c.recommend ';
 				$sql .= 'FROM images i, content c, content_images ci  where i.status = 1 AND i.id = ci.`imageId` AND ci.`contentId` = c.id order by i.`r_clicked` desc';
				
				if($discount > 0)
					$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
				while($rs && $row = mysql_fetch_array($rs)){
					$imgDto = new ImageDto();
	
					$imgDto->width			= $row[0];
					$imgDto->height			= $row[1];
					$imgDto->extName		= $row[2];
					$imgDto->fileSize		= $row[3];
					$imgDto->name			= $row[4];
					$imgDto->description	= $row[5];
					$imgDto->id 			= $row[6];
					$imgDto->md5			= $row[7];
					$imgDto->status			= $row[8];
					$imgDto->level			= $row[9];
					$imgDto->isAnimated		= $row[10];
					
					$dto = new ContentDto();
	
					$dto->id			= $row[11];
					$dto->name			= $row[12];
					$dto->description	= $row[13];
					
					$dto->status		= $row[14];
					$dto->createdTime	= $row[15];
					$dto->modifiedTime	= $row[16];
					
					$dto->r_imageCount	= $row[17];
					$dto->recommend		= $row[18];
					
					$dto->oImgs[] = $imgDto;
					
					$arrback[] = $dto;
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);	
		
		return $arrback;
	}
	
	public function getTopImages($contentId)
	{
		$arrback = array();
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$sql  = 'SELECT width, height, extName, fileSize, name, description, id, md5, status, level ';
			$sql .= "FROM images WHERE status = 1 AND id IN (SELECT imageId FROM content_images WHERE contentId = $contentId) ORDER BY level LIMIT 1";
					
			$rs  = mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
			while($rs && $row = mysql_fetch_array($rs)){
				$dto = new ImageDto();

				$dto->width			= $row[0];
				$dto->height		= $row[1];
				$dto->extName		= $row[2];
				$dto->fileSize		= $row[3];
				$dto->name			= $row[4];
				$dto->description	= $row[5];
				$dto->id 			= $row[6];
				$dto->md5			= $row[7];
				$dto->status		= $row[8];
				$dto->level			= $row[9];
				
				$arrback[] = $dto;
			}
			
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);
		
		return $arrback;	
	}
	
	public function getCustoms($where, $order, $page = 0, $discount = 20)
	{
		$arrback 	= array();
		$conn		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$sql 	= 'SELECT count(1) FROM content' . $where;
			
			$rs  	= mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if($this->isHaveData){				
				$sql  = 'SELECT id, name, description, status, createdTime, modifiedTime, r_imageCount, recommend ';
				$sql .= "FROM content $where $order";
				
				if($discount > 0)
					$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
				while($rs && $row = mysql_fetch_array($rs)){
					$dto = new ContentDto();
	
					$dto->id			= $row[0];
					$dto->name			= $row[1];
					$dto->description	= $row[2];
					
					$dto->status		= $row[3];
					$dto->createdTime	= $row[4];
					$dto->modifiedTime	= $row[5];
					
					$dto->r_imageCount	= $row[6];
					$dto->recommend		= $row[7];
					
					$arrback[] = $dto;
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);	
		
		return $arrback;
	}
	
	public function getAll()
	{
		$where = " WHERE status = 1";
		$oContents = $this->getCustoms($where, '', 1, 1000000);
		foreach($oContents as $oContent){
			$oContent->oImgs = $this->getImages($oContent->id);
		}
		
		return $oContents;
	}
	
	public function getContentIds($categoryId)
	{
		$sql = "SELECT contentId FROM content_category WHERE categoryId IN ($categoryId)";
		
		return $this->getColumns($this->DBTag, $sql);	
	}
	
	public function getContentTagIds($contentId)
	{
		$sql = "SELECT tagId FROM content_tags WHERE contentId IN ($contentId)";
		
		return $this->getColumns($this->DBTag, $sql);	
	}
	
	public function getImageTagIds($contentId)
	{
		$sql = "SELECT tagId FROM image_tags WHERE imageId IN (SELECT imageId FROM content_images WHERE contentId IN ($contentId))";
		
		return $this->getColumns($this->DBTag, $sql);	
	}
	
	public function getTagIds($contentId)
	{
		$sql = "SELECT tagId FROM content_tags WHERE contentId IN ($contentId)";
		
		return $this->getColumns($this->DBTag, $sql);	
	}
	
	public function getAllRecommendTop()
	{
		$where 		= " WHERE status = 1 AND recommend = 1 AND r_imageCount > 0";
		$order		= " ORDER BY id DESC";
		
		$page 		= 1;
		$discount 	= 6;
		
		$oContents = $this->getCustoms($where, $order, $page, $discount);
		foreach($oContents as $oContent){
			$oContent->oImgs = $this->getTopImages($oContent->id);
		}
		
		return $oContents;
	}
	
	public function getAllCategoryTop($categoryId, $discount = 6)
	{
		return $this->getAllCategories($categoryId, 1, $discount);		
	}
	
	public function getAllCategories($categoryId, $page, $discount)
	{
		if(empty($categoryId))
			return array();
			
		$where 	= " WHERE status = 1 AND r_imageCount > 0 AND id IN (SELECT contentId FROM content_category WHERE categoryId IN ($categoryId))";
		$order	= " ORDER BY id DESC";
		
		
		$oContents = $this->getCustoms($where, $order, $page, $discount);
		foreach($oContents as $oContent){
			$oContent->oImgs = $this->getTopImages($oContent->id);
		}
		
		return $oContents;
	}
	
	public function getTagContentPage($tagId, $page, $discount)
	{
		if(empty($tagId))
			return array();
			
		$where 	= " WHERE status = 1 AND r_imageCount > 0 AND id IN (SELECT contentId FROM content_tags WHERE tagId = $tagId)";
		$order	= " ORDER BY id DESC";
				
		$oContents = $this->getCustoms($where, $order, $page, $discount);
		foreach($oContents as $oContent){
			$oContent->oImgs = $this->getTopImages($oContent->id);
		}
		
		return $oContents;
	}
	
	public function getSearchContentPage($keyword, $page, $discount)
	{
		if(empty($keyword))
			return array();
		$keyword = addslashes(trim($keyword));
			
		$where 	= " WHERE status = 1 AND r_imageCount > 0 AND name like'%$keyword%'";
		$order	= " ORDER BY r_popular DESC";
				
		$oContents = $this->getCustoms($where, $order, $page, $discount);
		foreach($oContents as $oContent){
			$oContent->oImgs = $this->getTopImages($oContent->id);
		}
		
		return $oContents;
	}
	
	public function getPreId($arrImgIds, $imgId)
	{
		$imgIdx = array_search($imgId, $arrImgIds);
		
		if($imgIdx === 0 || $imgIdx === false)
			return -1;
			
		$preIdx = $imgIdx - 1;

		return $arrImgIds[$preIdx];
	}
	
	public function getNextId($arrImgIds, $imgId)
	{
		$len		= count($arrImgIds) - 1;
		$imgIdx 	= array_search($imgId, $arrImgIds);
		
		if($imgIdx === $len || $imgIdx === false)
			return -1;
			
		$nextIdx 	= $imgIdx + 1;
				
		$id = $arrImgIds[$nextIdx];
			
		return $id;
	}
	
	
	public function onClick($contentId)
	{
		$sql = "INSERT INTO content SET r_clicked = 1, id = $contentId ON DUPLICATE KEY UPDATE r_clicked = r_clicked + 1";
		$this->update($this->DBTag, $sql);
	}
}
?>
